package com.jinbo.footmessageworker.httpretrofit;


import com.jinbo.footmessageworker.httpbean.BaseBean;
import com.jinbo.footmessageworker.httpbean.LookSignBean;
import com.jinbo.footmessageworker.httpbean.OrdersBean;
import com.jinbo.footmessageworker.httpbean.SignBean;
import com.jinbo.footmessageworker.httpbean.TurnsBean;
import com.jinbo.footmessageworker.httpbean.WorkerInformationBean;

import java.io.File;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedFile;


public class BusinessController {

    private HttpBusinessStore mRemoteBusinessStore;

    private BusinessController() {
    }

    public static BusinessController getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        public static final BusinessController INSTANCE = new BusinessController();
    }

    public HttpBusinessStore getRemoteBusinessStore() {
        if (mRemoteBusinessStore == null) {
            mRemoteBusinessStore = new HttpBusinessStore();
        }
        return mRemoteBusinessStore;
    }

    /**
     * 登录
     *
     * @param name
     * @param password
     * @param listener
     */
    public void login(final String name, final String password, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().login(name, password,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 修改密码
     *
     * @param user_id      令牌
     * @param old_password 旧密码
     * @param new_password 新密码
     * @param listener
     */
    public void modifyPassword(final String user_id, final String old_password, final String new_password, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyUserPassword(user_id, old_password, new_password,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 修改头像
     *
     * @param user_id  //  令牌
     * @param imgPath  // 图片路径
     * @param kind
     * @param listener
     */
    public void modifyFace(final String user_id, final String imgPath, final int kind, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            String tempImagePath = imgPath;
            if (tempImagePath.startsWith("file://")) {
                tempImagePath = imgPath.replace("file://", "");
            }
            final File file = new File(tempImagePath);// BitmapUtils.getCompressImage(tempImagePath);
            //Log.i("Gunter", "file:" + file.getPath());
            getRemoteBusinessStore().modifyFace(user_id, new TypedFile("image/png", file), kind,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 修改用户信息
     *
     * @param user_id
     * @param project_ids
     * @param user_kind
     * @param listener
     */
    public void modifyUserInformation(final String user_id, final String project_ids, final int user_kind, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyInformation(user_id, project_ids, user_kind,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 修改项目
     *
     * @param user_id
     * @param project_ids
     * @param listener
     */
    public void modifyProjects(final String user_id, final String project_ids, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().modifyProjects(user_id, project_ids,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 获取项目
     */
    public void getProjects(final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().getProjects(
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 获取我的项目
     *
     * @param user_id
     */
    public void getMyProjects(final String user_id, final Listener<WorkerInformationBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().getMyProjects(user_id,
                    new Callback<BaseBean<WorkerInformationBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<WorkerInformationBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }


    /**
     * 技师查询待确认订单
     *
     * @param worker_id
     */
    public void notConfirmOrder(final String worker_id, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().notConfirmOrder(worker_id,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 技师接收待确认的单
     *
     * @param id
     */
    public void sureOrder(final int id, final String do_time, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().sureOrder(id, do_time,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 技师拒绝待确认的单
     *
     * @param id
     */

    public void rejectOrder(final int id, final String do_time, final String reason, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().rejectOrder(id, do_time, reason,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 技师查询已接订单
     *
     * @param worker_id
     */
    public void dealtOrder(final String worker_id, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().dealtOrder(worker_id,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 技师完成接收的单
     *
     * @param id
     */
    public void completeOrder(final int id, final String do_time, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().completeOrder(id, do_time,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 技师撤销已接的单
     *
     * @param id
     */
    public void cancelOrder(final int id, final String do_time, final String reason, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().cancelOrder(id, do_time, reason,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 查询历史订单
     *
     * @param worker_id
     */
    public void historyOrder(final String worker_id, final int start, final int limit, final Listener<OrdersBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().historyOrder(worker_id, start, limit,
                    new Callback<BaseBean<OrdersBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<OrdersBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }


    /**
     * 查询班次
     *
     * @param worker_id
     */
    public void turn(final String worker_id, final int start, final int limit, final Listener<TurnsBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().turn(worker_id, start, limit,
                    new Callback<BaseBean<TurnsBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<TurnsBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }

    /**
     * 签到
     *
     * @param worker_id
     */
    public void sign(final String worker_id, final double longitude, final double latitude,
                     final String time,
                     final String date,
                     final int kind,
                     final Listener<SignBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().sign(worker_id, longitude, latitude,
                    time,
                    date,
                    kind,
                    new Callback<BaseBean<SignBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<SignBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }


    /**
     * 查询上下班签到
     *
     * @param worker_id
     */
    public void lookSign(final String worker_id,
                     final String date,
                     final Listener<LookSignBean> listener) {
        if (listener != null) {
            listener.onStart();
        }
        try {
            getRemoteBusinessStore().lookSign(worker_id,
                    date,
                    new Callback<BaseBean<LookSignBean>>() {
                        @Override
                        public void failure(RetrofitError arg0) {
                            if (listener != null) {
                                listener.onFail(ErrorUtil.getNetworkError());
                            }
                        }

                        @Override
                        public void success(BaseBean<LookSignBean> result,
                                            Response arg1) {
                            if (result != null) {
                                if (result.ok()) {


                                    if (listener != null) {
                                        listener.onComplete(result.data);
                                    }
                                } else {
                                    if (listener != null) {
                                        listener.onFail(ErrorUtil
                                                .getServerError(result));

                                    }
                                }
                            } else {
                                if (listener != null) {
                                    listener.onFail(
                                            ErrorUtil.getNetworkError());
                                }
                            }
                        }
                    });
        } catch (Exception e) {
            if (listener != null) {
                listener.onFail(ErrorUtil.getNetworkError());
            }
        }
    }
}
